#!/bin/sh

# 注入环境变量
. $CI_SHELL_PATH/scripts/build-env-generate.sh

set +xe

project=$1
env=$2
git_status_file="git-status-${project}-${env}.txt"

if [ -e $git_status_file ]; then
  status_string="\n查看本次变更 https://f2e.qtrade.com.cn/sourcemap/git-status/${git_status_file}"
fi

common_string="author: @${GITLAB_USER_NAME}\nproject: ${project}\nenv: ${env}\nversion: ${ROLLBACK_VERSION_ID}\n编译完成，请将本信息转发告诉"
common_string2="前往执行jenkins\nhttps://ci.qtrade.com.cn/jenkins/job/fe-${env}-qtrade-ci/build"


# if env is dev trigger jenkins webhook
if [ $env = "dev" ]; then
  curl -u redactedUser:redactedToken --show-error -H 'Content-Type:application/json' --user "lvben:${LVBEN_JENKINS_API_TOKEN}" -X POST "https://ci.qtrade.com.cn/jenkins/job/fe-dev-qtrade-ci/buildWithParameters?token=efawefwefawef&project=${project}&env=${env}&version=${ROLLBACK_VERSION_ID}"

  # TODO: 上面 curl 出错了没有告知
  echo "finish compiling and triggering jenkins to deploy"

  if [ $CI_RUNNER_ID = "24" ]; then
    $CI_SHELL_PATH/scripts/utils/send-wecom-notify.sh fc421173-b498-438b-a8d3-f88560b719bf "$common_string runner notify"
  fi

elif [ $env = "qa" ] || [ $env = "uat" ]; then

  $CI_SHELL_PATH/scripts/utils/send-wecom-notify.sh $WECOM_BOT_KEY_1 "$common_string测试同学$common_string2$status_string"

  echo "finish triggering wecom webhook"

elif [ $env = "prl" ]; then

  $CI_SHELL_PATH/scripts/utils/send-wecom-notify.sh $WECOM_BOT_KEY_1 "$common_string有权限部署的同学$common_string2"

  echo "finish triggering wecom webhook"
elif [ $env = "prod" ]; then

  $CI_SHELL_PATH/scripts/utils/send-wecom-notify.sh $WECOM_BOT_KEY_1 "$common_string有权限部署的同学$common_string2"
  echo "finish triggering wecom webhook"
fi
